/**
 * 管理初始化
 */
var Grid = {
    id: "gridTable", //表格id
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1,
    title:"表格", //表头
    width:"800px",
    height:"450px",
    controllerName:"", //请求地址
    PrimaryKey:""//表对象主健
};


/**
 * 检查是否选中
 */
Grid.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Common.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Grid.seItem = selected[0];
        return true;
    }
};

Grid.openAdd = function(){
    var index = layer.open({
        type: 2,
        title: Grid.title,
        area: [Grid.width, Grid.height], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Common.ctxPath + Grid.controllerName +'/to_add'
    });
    this.layerIndex = index;
}

Grid.openEdit = function(){
    if (this.check()) {
        var Id = this.seItem[Grid.PrimaryKey];
        var index = layer.open({
            type: 2,
            title: Grid.title,
            area: [Grid.width, Grid.height], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Common.ctxPath + Grid.controllerName + '/to_update/' + Id
        });
        this.layerIndex = index;
    }

}

Grid.delete = function () {
    if (this.check()) {
        var Id = this.seItem[Grid.PrimaryKey];
        var operation = function(){
            var ajax = new $ax(Common.ctxPath + Grid.controllerName + "/remove", function (result) {
                if (result.success) {
                    Common.success(result.message + "!");
                    Grid.table.refresh();
                } else {
                    Common.error(result.message + "!");
                }
            }, function (result) {
                Common.error(result.responseJSON.message + "!");
            });
            ajax.set(Grid.PrimaryKey, Id);
            ajax.start();
        };
        Common.confirm("是否删除该记录？",operation);
    }
};

/**
 * 重置搜索
 */
Grid.resetSearch = function () {
    $("#condition").val("");
    $("#beginTime").val("");
    $("#endTime").val("");
    Grid.search();
}

/**
 * 查询列表
 */
Grid.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();
    Grid.table.refresh({query: queryData});
};
